package com.lsa.southwind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsa.southwind.entity.Building;
import com.lsa.southwind.entity.Dormitory;
import com.lsa.southwind.entity.Student;
import com.lsa.southwind.form.SearchForm;
import com.lsa.southwind.mapper.BuildingMapper;
import com.lsa.southwind.mapper.DormitoryMapper;
import com.lsa.southwind.mapper.StudentMapper;
import com.lsa.southwind.service.DormitoryService;
import com.lsa.southwind.util.ResultVoUtil;
import com.lsa.southwind.vo.DormitoryVo;
import com.lsa.southwind.vo.PageVo;
import com.lsa.southwind.vo.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2023-02-25
 */
@Service
public class DormitoryServiceImpl extends ServiceImpl<DormitoryMapper, Dormitory> implements DormitoryService {
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public ResultVo availableList() {
        QueryWrapper<Dormitory> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("available", 0);
        List<Dormitory> dormitoryList = this.dormitoryMapper.selectList(queryWrapper);
        System.out.println("查询的数据:" + dormitoryList);
        if (dormitoryList == null) {
            return ResultVoUtil.fail();
        } else {

            return ResultVoUtil.success(dormitoryList);
        }

    }

    /*列表查询*/
    @Override
    public PageVo list(Integer page, Integer size) {
        /*分页设置*/
        Page<Dormitory> dormitoryPage = new Page<>(page, size);
        Page<Dormitory> dormitoryPageList = this.dormitoryMapper.selectPage(dormitoryPage, null);
        List<DormitoryVo> dormitoryVoList = new ArrayList<>();
        for (Dormitory dormitory : dormitoryPageList.getRecords()) {
            /*因为前端展示的是名字，而数据库查询出来的是数据所有进行值的转换*/
            DormitoryVo dormitoryVo = new DormitoryVo();
            /*把查出的属性值，赋值给dormitoryVo*/
            BeanUtils.copyProperties(dormitory, dormitoryVo);
            Building building = this.buildingMapper.selectById(dormitory.getBuildingId());
            dormitoryVo.setBuildingName(building.getName());
            dormitoryVoList.add(dormitoryVo);
        }
        PageVo pageVo = new PageVo();
        pageVo.setData(dormitoryVoList);
        pageVo.setTotal(dormitoryPageList.getTotal());
        return pageVo;
    }

    @Override
    public PageVo search(SearchForm searchForm) {
        Page<Dormitory> dormitoryPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<Dormitory> dormitoryPageList = null;
        if (searchForm.getValue().equals("")) {
            dormitoryPageList = this.dormitoryMapper.selectPage(dormitoryPage, null);
        } else {
            QueryWrapper<Dormitory> dormitoryQueryWrapper = new QueryWrapper<>();
            dormitoryQueryWrapper.like(searchForm.getKey(), searchForm.getValue());
            dormitoryPageList = this.dormitoryMapper.selectPage(dormitoryPage, dormitoryQueryWrapper);
        }
        List<DormitoryVo> dormitoryVoList = new ArrayList<>();
        for (Dormitory dormitory : dormitoryPageList.getRecords()) {
            DormitoryVo dormitoryVo = new DormitoryVo();
            BeanUtils.copyProperties(dormitory, dormitoryVo);
            Building building = this.buildingMapper.selectById(dormitory.getBuildingId());
            dormitoryVo.setBuildingName(building.getName());
            dormitoryVoList.add(dormitoryVo);
        }

        PageVo pageVo = new PageVo();
        pageVo.setData(dormitoryVoList);
        pageVo.setTotal(dormitoryPageList.getTotal());
        return pageVo;
    }

    @Override
    public Boolean deleteById(Integer id) {
        /*
         * 1.首先看该宿舍有没有学生
         * 2.如果有学生就不删除
         * 3.通过判断设置的类型数与实际人数是否相同
         * 如果相同说明没有住人，如果不同说明住人了
         */
        /*第一种方法*/
//        Dormitory dormitoryById = this.dormitoryMapper.selectById(id);
//        if (dormitoryById.getType().equals(dormitoryById.getAvailable())) {
//            //如果设置的数量和房间人数相等，就是没人允许删除
//            int deleteById = this.dormitoryMapper.deleteById(id);
//            /*成功为1 失败为0*/
//            if (deleteById != 1) {
//                return false;
//            } else {
//                return true;
//            }
//        } else {
//            return false;
//        }
        /*第二种方法*/
        /*
         * 1.通过宿舍id查出所有学生
         * 2.查出与他相同楼宇，除了该宿舍以为的宿舍
         * 3.给学生换宿舍
         * 4.删除宿舍
         */
        //1. 通过宿舍id查出该所有学生
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Student> dormitoryId = studentQueryWrapper.eq("dormitory_id", id);
        List<Student> studentList = this.studentMapper.selectList(dormitoryId);
        Dormitory dormitory = this.dormitoryMapper.selectById(id);
        for (Student student : studentList) {
            //2.查出与他相同楼宇，除了该宿舍以为的宿舍
            Integer availableDormitoryId = this.dormitoryMapper.selectAvailableDormitoryId(id, dormitory.getBuildingId());
            student.setDormitoryId(availableDormitoryId);
            try {
                int updateById = this.studentMapper.updateById(student);
                if (updateById != 1) return false;
                this.dormitoryMapper.subAvailable(availableDormitoryId);
            } catch (Exception e) {
                return false;
            }
        }
        int deleteById = this.dormitoryMapper.deleteById(id);
        if (deleteById != 1) {
            return false;
        } else {
            return true;
        }
    }


}
